package com.surfilter.service.impl;

import com.github.pagehelper.PageHelper;
import com.surfilter.bean.QueryResult;
import com.surfilter.changesource.LayuiPageInfo;
import com.surfilter.dao.IdentityConfigMapper;
import com.surfilter.service.IdentityConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

/**
 * @Author: lijia
 * @Date: Created in  2018-05-11 15:48
 * @Description:
 */

@Service
public class IdentityConfigServiceImpl implements IdentityConfigService{

    @Autowired
    private IdentityConfigMapper identityConfigMapper;

    @Override
    public Object queryTypeResult(QueryResult queryResult) {
        PageHelper.startPage(queryResult.getPageNum(), queryResult.getLimit());
        List<QueryResult> list = identityConfigMapper.queryTypeResult(queryResult);
        LayuiPageInfo<QueryResult> pageInfo = new LayuiPageInfo<QueryResult>(list);
        return pageInfo;
    }

    @Override
    public int deteleVirtualTypeById(String id) {
        return identityConfigMapper.deteleVirtualTypeById(id);
    }

    @Override
    @Transactional
    public int addVirtualtype(MultipartFile file, QueryResult queryResult) {



        if(identityConfigMapper.selectPicturePathIsExist(file.getOriginalFilename()) != 0){
            return -2;
        }

        //上传虚拟身份规则图片
        if(!uploadPicture(file,queryResult.getVirtual_Name())){
            return -1;//添加失败
        }


        if(identityConfigMapper.selectVirtualNameIsExist(queryResult.getVirtual_Name()) != 0){
            return 0;
        }

        return identityConfigMapper.addVirtualtype(queryResult);
    }



    /**
     * 上传虚拟身份规则图片
     * @param upload
     * @param uploadFileName
     * @return
     */
    private boolean uploadPicture(MultipartFile upload, String uploadFileName){
        boolean bool = true;
        BufferedInputStream in=null;
        BufferedOutputStream out=null;
        try {

            File file = new File(ResourceUtils.getURL("classpath:").getPath()+"/static/picture/"+upload.getOriginalFilename());
            if (!file.exists()) {
                file.createNewFile();
            }
            in = new BufferedInputStream(upload.getInputStream());
            //字节流转图片对象
            Image bi = ImageIO.read(in);
            //获取图像的高度，宽度
//              int height=bi.getHeight(null);
//              int width =bi.getWidth(null);
            //构建图片流
            BufferedImage tag = new BufferedImage(40, 40, BufferedImage.TYPE_INT_RGB);
            //绘制改变尺寸后的图
            tag.getGraphics().drawImage(bi, 0, 0,40, 40, null);
            //输出流
            out = new BufferedOutputStream(new FileOutputStream(file));
            //JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
            //encoder.encode(tag);
            ImageIO.write(tag, "PNG",out);
            out.flush();
            in.close();
            out.close();
        } catch (Exception e) {
            bool = false;
            e.printStackTrace();
        } finally {
            try {
                if(in != null){
                    in.close();
                }
                if(out != null){
                    out.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return bool;
    }

}
